var SWINGFLAG = 1;
var CURRENTBAR;

function swing() {
	if(SWINGFLAG == 1) {
		document.getElementById('welcome').style.transform = 'rotate(10deg)';
		setTimeout("document.getElementById('welcome').style.transform='rotate(-10deg)'", 1500);
	}else{
		document.getElementById('welcome').style.transform = 'rotate(10deg) scale(0.5)';
		setTimeout("document.getElementById('welcome').style.transform='rotate(-10deg) scale(0.5)'", 1500);
	}
};

function createXMLHttpRequest() { //万能模板

    var XMLHttpReq;

    if(window.XMLHttpRequest) { // 是Mozilla浏览器

        XMLHttpReq = new XMLHttpRequest();
    } else if(window.ActiveXObject()) { // IE浏览器
        try {
            XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); //因为ie也会出现不兼容所以抛异常
        } catch(e) {
            XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); //微软的，记住就好
        }
    }
    return XMLHttpReq;
}

function getMessage() {
    // $.sessionStorage.set('User',account);
    // alert("ss");
    $("#myclass1").empty();
    $("#flade").show();
    var xmlReq = createXMLHttpRequest();
    xmlReq.open("GET", "http://localhost:8080/ClassBelong", true);
    xmlReq.send();
    xmlReq.onreadystatechange = function() {
        if(xmlReq.readyState == 4) { //判断对象状态是否完成
            if(xmlReq.status == 200) { //信息已经成功返回
                var myObj = JSON.parse(xmlReq.responseText);
                // alert(xmlReq.responseText);

                for( a of myObj.classArr){
                	var cla = "<p id='" + a.class_key + "' onclick='getStu(this)' >" + a.class_name + "</p>"
                    $('#myclass1').append($(cla))

				}

            }
        }
    }
}

function getTask() {
    // $.sessionStorage.set('User',account);
    // alert("ss");

    var InviteID1 = document.getElementById('InviteID1').value;
    var xmlReq = createXMLHttpRequest();
    xmlReq.open("GET", "http://localhost:8080/showTask?InviteID1="+InviteID1, true);
    // xmlReq.open("GET", "http://localhost:8080/showTask?InviteID1=5rcomh", true);
    xmlReq.send();
    xmlReq.onreadystatechange = function() {
        if(xmlReq.readyState == 4) { //判断对象状态是否完成
            if(xmlReq.status == 200) { //信息已经成功返回
                // var myObj = JSON.parse(xmlReq.responseText);
                // alert(xmlReq.responseText);
                document.getElementById("myclass2").innerHTML=xmlReq.responseText;
            }
        }
    }
}



window.onload = function () {

    document.getElementById("addC").onclick = function() {

        var classname = document.getElementById('classname').value;
        // alert(classname);

        var xmlReq = createXMLHttpRequest();

        xmlReq.open("GET", "http://localhost:8080/Group?classname="+ classname, true);
        xmlReq.send();


        xmlReq.onreadystatechange = function() {
            if(xmlReq.readyState == 4) { //判断对象状态是否完成
                if(xmlReq.status == 200) { //信息已经成功返回
                    // var xmltext = xmlReq.responseText; //得到AjaxServlet往外输出的数据
                    alert("创建成功!");
                }

            }
        }

        // console.log(classowner)

        // var a = $.ajax({
        //     async: false,
        //     url: "http://localhost:8080/Group",
        //     data: {'cam': classname, 'cao': classowner}
        // });
        // console.log(a);
        // var b = JSON.parse(a['responseText']);s
        // console.log(b);
        // console.log(b['用户名'])
    }

    document.getElementById("createT").onclick = function() {

        var inviteID = document.getElementById('inviteID').value;
        var taskTheme = document.getElementById('taskTheme').value;
        var taskDeadline = document.getElementById('taskDeadline').value;
        //前面数据传过来是一整串
        var taskContent = document.getElementById('taskContent').value;

        var xmlReq = createXMLHttpRequest();

        xmlReq.open("GET", "http://localhost:8080/createTask?inviteID="+ inviteID+"&taskTheme="+taskTheme+"&taskDeadline="+taskDeadline+"&taskContent="+taskContent, true);
        xmlReq.send();


        xmlReq.onreadystatechange = function() {
            if(xmlReq.readyState == 4) { //判断对象状态是否完成
                if(xmlReq.status == 200) { //信息已经成功返回
                    // var xmltext = xmlReq.responseText; //得到AjaxServlet往外输出的数据
                    alert("创建成功!");
                }

            }
        }
    }
    document.getElementById("ShowS").onclick = function() {

        var inviteID = document.getElementById('inviteID3').value;
        // alert(inviteID);
        var xmlReq = createXMLHttpRequest();

        xmlReq.open("GET", "http://localhost:8080/ShowClassStu?inviteID3="+inviteID, true);
        xmlReq.send();


        xmlReq.onreadystatechange = function() {
            if(xmlReq.readyState == 4) { //判断对象状态是否完成
                if(xmlReq.status == 200) { //信息已经成功返回
                    // var xmltext = xmlReq.responseText; //得到AjaxServlet往外输出的数据
                    // var xmltext = xmlReq.responseText;
                    // alert(xmltext);

                    document.getElementById("myclass1").innerHTML=xmlReq.responseText;
                    $("#flade").hide()


                }

            }
        }
    }


}





function getStu(a) {
    document.getElementById('inviteID3').value = $(a).attr('id');
	// console.log($(a).attr('id'))
	// $.ajax({
	// 	async:false,
	// 	data:
	// })


}

function getStu2(a) {
    document.getElementById('InviteID1').value = $(a).attr('id');
    // console.log($(a).attr('id'))
    // $.ajax({
    // 	async:false,
    // 	data:
    // })


}



$(function() {
	
	$(".content").hide(); 
	$(".content").removeAttr("hidden");    //撤销hidden属性使之可以显示且不会提前闪现
	
	setTimeout("document.getElementById('welcome').style.transform='rotate(-10deg)'", 500);
	setInterval("swing()", 3000);
	//	console.log($("welcome"));
	//welcome标题栏的左右摇摆

	$("#menuBar").click(() => {
		SWINGFLAG = 0;		//更改摇摆时的大小
		$('#welcome').animate({
			top: '5%',
			left: '-8%'
		}, 1500);

		$('#menuBar').animate({
			left: '5%',
			width: '10%'
		}).css({
			'transform': 'rotate(360deg)',
			'top': '25%'
		})
	});
	//点击菜单栏后的菜单和标题的移动动画

	$(".bar").hover(function() {
		let nam = 'image/bar/bar' + $(this).attr('no') + 'x.png';
		$(this).attr('src', nam).attr('width', "100%")
	}, function() {
		//		console.log($(this).attr('flag'))
		if($(this).attr('flag') != "1") {
			let nam = 'image/bar/bar' + $(this).attr('no') + '.png';
			$(this).attr('src', nam).attr('width', "50%")
		} else {
			$(this).attr('width', "80%")
		}
	});
	//设置菜单栏单项的鼠标移入移出事件，当前点击对应移出只改大小不改图片

	$(".bar").click(function() {
		for(item of $(this).siblings()) {
			if($(item).attr('flag') == '1') {
				$(item).attr('src', 'image/bar/bar' + $(item).attr('no') + '.png')
				$(item).attr('width', "50%")
			}
		}
		$(this).attr('flag', 1);
		$(this).siblings().attr('flag', 0);
		
		CURRENTBAR = $(this).attr('no');
		$(".content").hide();
		$(".content")[parseInt(CURRENTBAR)-1].style.display = '';
	});
	//通过添加flag属性为1或取消为0，标记当前点击菜单项

	$("#exit").hover(function() {
		$(this).attr('src', 'image/bar/bar0x.png')
	}, function() {
		$(this).attr('src', 'image/bar/bar0.png')
	});

	//单独对exit退出选项的移入特效
})



function getMessage2() {
    // $.sessionStorage.set('User',account);
    // alert("ss");
    $("#myclass2").empty();
    $("#flade2").show();
    var xmlReq = createXMLHttpRequest();
    xmlReq.open("GET", "http://localhost:8080/ClassBelong", true);
    xmlReq.send();
    xmlReq.onreadystatechange = function() {
        if(xmlReq.readyState == 4) { //判断对象状态是否完成
            if(xmlReq.status == 200) { //信息已经成功返回
                var myObj = JSON.parse(xmlReq.responseText);
                // alert(xmlReq.responseText);

                for( a of myObj.classArr){
                    var cla = "<p id='" + a.class_key + "' onclick='getStu2(this)' >" + a.class_name + "</p>"
                    $('#myclass2').append($(cla))

                }

            }
        }
    }
}
